package mytestjasperreportproject

import exceptions.ExportException
import export.ExportType
import export.ExportHandlerImpl

class TestExportService {
	
	/**
	* Return the widths of columns in the export file when export list of domains.
	*/
   protected Long[] getColWidth() {
	   return [300, 300] as Long[]
   }
   
   /**
	* Return the column's headers in the export file when export list of domains.
	* */
   protected String[] getColTitles() {
	   return ['Id', 'Name'] as String[]
   }
   
   /**
	* Return the fields of domain to export.
	* */
   protected String[] getColNames() {
	   return ['id','name'] as String[]
   }
   
   /**
	* Return the type of columns when export list of domains.
	* */
   protected Class<?>[] getExportColType() {
	   return [String.class, String.class]as Class<?>[]
   }

    public void export(def data, String filename, String basePath, ExportType type, String[] sheetNames) throws ExportException {
        // Firstly: Prepare colTypes to export
        def colTypes = getExportColType()

        // Secondly : Prepare colNames , and this must match with properties in exported DTO
        def colNames = getColNames()
        // Prepare column titles which will be shown on EXCEL file exported.
        def colTitles = getColTitles()
        // The width of columns in excel file
        int[] colWidths = getColWidth()

        // Initialize export utility service
        ExportHandlerImpl exportHandlerImpl = new ExportHandlerImpl()

        // Begin export parameters
        try {
            exportHandlerImpl.exportHandler(filename, basePath, type, data, colNames, colTypes,
                    colTitles, colWidths, sheetNames)
        } catch (Exception e) {
            log.error ('Error exporting handler', e)
            throw new ExportException("Unable to export data ", e)
        }
    }
}
